package com.lixiang.userinfo.controller;

import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.lixiang.userinfo.entity.User;
import com.lixiang.userinfo.service.IUserService;
import com.lixiang.userinfo.util.ApiResult;
import com.lixiang.userinfo.util.ResultCode;

/**
 * 客户信息控制器
 * @author 李湘
 * @date 2018-10-19
 */
//证明是controller层并且返回json
@RestController
public class UserController {
	
	//日志记录器
	private static Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private IUserService userService;//自动注入依赖
	
	/**
	 * 录入客户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("user/save")
	public @ResponseBody ApiResult save(User user,int saleId){
		try {
			System.out.println(user);
			int rowCount=userService.insertSelective(user);
			if(rowCount==1){
				return ApiResult.success();
			}
		} catch (Exception e) {
			logger.error("录入客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 删除客户信息 将状态置为2 禁用
	 * @param userId
	 * @return
	 */
	@RequestMapping("user/delete/{userId}")
	@ResponseBody
	public ApiResult delete(@PathVariable("userId")int userId){
		try {
			int rowCount=userService.delete(userId);
			if(rowCount==1){
				return ApiResult.success();
			}
		} catch (Exception e) {
			logger.error("删除客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 更新编辑客户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("user/update")
	@ResponseBody
	public ApiResult update(User user){
		try {
			int rowCount=userService.updateByPrimaryKeySelective(user);
			if(rowCount==1){
				return ApiResult.success();
			}
		} catch (Exception e) {
			logger.error("更新客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 按周查询客户信息（分页）
	 * @param page
	 * @return
	 */
	@RequestMapping(value="user/selectWeek/{page}")
	@ResponseBody
	public ApiResult selectWeek(@PathVariable("page")int page){
		try {
			PageInfo<User> pageInfo=userService.selectWeek(page);
			return ApiResult.success(pageInfo);
		} catch (Exception e) {
			logger.error("按周查询客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 按月查询客户信息（分页）
	 * @param page
	 * @return
	 */
	@RequestMapping(value="user/selectMonth/{page}")
	@ResponseBody
	public ApiResult selectMonth(@PathVariable("page")int page){
		try {
			PageInfo<User> pageInfo=userService.selectMonth(page);
			return ApiResult.success(pageInfo);
		} catch (Exception e) {
			logger.error("按月查询客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 按年查询客户信息（分页）
	 * @param page
	 * @return
	 */
	@RequestMapping(value="user/selectYear/{page}")
	@ResponseBody
	public ApiResult selectYear(@PathVariable("page")int page){
		try {
			PageInfo<User> pageInfo=userService.selectYear(page);
			return ApiResult.success(pageInfo);
		} catch (Exception e) {
			logger.error("按年查询客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	/**
	 * 指定日期查询客户信息
	 * @param userLastlogtime 指定日期
	 * @return
	 */
	@RequestMapping(value="user/selectLogtime/{page}/{userLastlogtime}")
	@ResponseBody
	public ApiResult selectLogtime(@PathVariable("page")int page,@PathVariable("userLastlogtime")String userLastlogtime){
		try {
			PageInfo<User> pageInfo= userService.selectLogtime(page,userLastlogtime);
			return ApiResult.success(pageInfo);
		} catch (Exception e) {
			logger.error("指定日期查询客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
	
	/**
	 * 分页查询客户信息
	 * @param page
	 * @param userName
	 * @param userPhone
	 * @param userSex
	 * @param userLogcountInteger
	 * @param saleId
	 * @return
	 */
	@RequestMapping(value="user/selectAll/{page}")
	@ResponseBody
	public ApiResult selectAll(@PathVariable("page")int page,String userName,String userPhone,Integer userSex,Integer userLogcountInteger,Integer saleId){
		try {
			//组织查询参数
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("userName", userName);
			map.put("userPhone", userPhone);
			map.put("userSex", userSex);
			map.put("userLogcountInteger", userLogcountInteger);
			map.put("saleId", saleId);
			
			PageInfo<User> pageInfo=userService.selectAll(page,map);
			return ApiResult.success(pageInfo);
		} catch (Exception e) {
			logger.error("查询客户信息错误="+e.getMessage());
		}
		return ApiResult.failure(ResultCode.DATA_IS_WRONG);
	}
	
}
